var xmlhttp;
var receiveid;
var seslocid = '';
var sesAcname = '';
var sesuserid = '';
var sesgrpid = '';
var sesusername = '';
var seslocationtype = '';
var sescountryid = '';
var sesfinancialyear = '';
var sesAccountId = '';
var Conn1_ConnectionString = '';
var DBName = '';
var DBOName = '';
var sesRoundOffId = '';
var seslocname = '';

$(document).ready(function () {
    $('#txtfind').keyup(function (e) {
       // var unicode = e.charCode ? e.charCode : e.keyCode;
       // if (unicode == 13) {
            search();
       // }
    })

})
function addClass() {
    var inp_ele = document.getElementsByTagName('input');
    var sel_ele = document.getElementsByTagName('select');
    var btn_ele = document.getElementsByTagName('button');
    for (var i = 0; i < inp_ele.length; i++) {
        if (inp_ele[i].type == 'text') {
            inp_ele[i].setAttribute('class', 'asptxtbox');
        }
    }
    for (var i = 0; i < btn_ele.length; i++) {
        if (btn_ele[i].type == 'button') {
            btn_ele[i].removeAttribute('class');
            btn_ele[i].setAttribute('class', 'aspbutton');
        }
    }
    for (var i = 0; i < sel_ele.length; i++) {
        if (sel_ele[i].getAttribute('multiple')) {
            sel_ele[i].setAttribute('class', 'aspmslctbox');
        }
        else {

            if (sel_ele[i].id != "jacsMonths" && sel_ele[i].id != "jacsYears") {
                sel_ele[i].setAttribute('class', 'aspslctbox');
            }
        }
    }
}
function wait_till_complete(_status) {
    if (parseInt(_status) == 4) {
        var _d_full_find = "";
        if (document.getElementById('_d_full_progress')) {
            _d_full_find = document.getElementById('_d_full_progress')
            document.body.removeChild(_d_full_find);
        }
    }
    if (parseInt(_status) == 1) {
        var _d_full = document.createElement('div');
        _d_full.id = "_d_full_progress";
        _d_full.innerHTML = '<div align="center" style="border:0px solid #39579c;margin:auto;height:100px; width:200px;position:fixed;top:40%;left:40%;padding:auto;background-color:Gray; color:#000"><div><img src="images/loading.gif" style="padding-top:20px" alt="Progress..." /></div><div>Progress....</div></div>';
        _d_full.setAttribute("style", "height:100%; width:100%;background-color:Gray;opacity:0.65;opacity:filter: alpha(opacity=65); position:fixed;top:0px;left:0px;");
        if (!(document.getElementById('_d_full_progress'))) {
            document.body.appendChild(_d_full)
        }
    }
}
function parse_json(data) {
    var fill;
    try {
        fill = eval('(' + xmlhttp.responseText + ')');
    }
    catch (e) {
        fill = eval('({})');
    }
    return fill;
}
function setVarBlank() {
    seslocid = '';
    sesAcname = '';
    sesuserid = '';
    sesgrpid = '';
    sesusername = '';
    seslocationtype = '';
    sescountryid = '';
    sesfinancialyear = '';
    sesAccountId = '';
    Conn1_ConnectionString = '';
    DBName = '';
    DBOName = '';
    sesRoundOffId = '';
    seslocname = '';
}
function set_gVal() {
    if (seslocid == '') { seslocid = document.getElementById("seslocid").value; }
    if (sesAcname == '') { sesAcname = document.getElementById("sesAcname").value; }
    if (sesuserid == '') { sesuserid = document.getElementById("sesuserid").value; }
    if (sesgrpid == '') { sesgrpid = document.getElementById("sesgrpid").value; }
    if (sesusername == '') { sesusername = document.getElementById("sesusername").value; }
    if (seslocationtype == '') { seslocationtype = document.getElementById("seslocationtype").value; }
    if (sescountryid == '') { sescountryid = document.getElementById("sescountryid").value; }
    if (sesfinancialyear == '') { sesfinancialyear = document.getElementById("sesfinancialyear").value; }
    if (sesAccountId == '') { sesAccountId = document.getElementById("sesAccountId").value; }
    if (Conn1_ConnectionString == '') { Conn1_ConnectionString = document.getElementById("Conn1_ConnectionString").value; }
    if (DBName == '') { DBName = document.getElementById("DBName").value; }
    if (DBOName == '') { DBOName = document.getElementById("DBOName").value; }
    if (sesRoundOffId == '') { sesRoundOffId = document.getElementById("sesRoundOffId").value; }
    if (seslocname == '') { seslocname = document.getElementById("seslocname").value; }
}

function senddata_post(pagen, str, rid) {

    //alert(pagen + "pagen" + str + "str" + rid + "rid");

    set_gVal();
    if (seslocid == '') {
        alert("Your session has been expired.You need to login first.")
        window.location.href = "Default.aspx"
        return;
    }
    else {
        document.getElementById("serror").innerHTML = '';
        receiveid = rid;
        xmlhttp = GetXmlHttpObject();
        if (xmlhttp == null) {
            alert("Your browser does not support AJAX!");
            return;
        }
        var url;
        url = "ajax/" + pagen + ".aspx?seslocid=" + seslocid + '&sesAcname=' + sesAcname + '&sesuserid=' + sesuserid + '&sesgrpid=' + sesgrpid + '&sesusername=' + sesusername + '&seslocationtype=' + seslocationtype + '&sescountryid=' + sescountryid + '&sesfinancialyear=' + sesfinancialyear + '&sesAccountId=' + sesAccountId + '&Conn1_ConnectionString=' + Conn1_ConnectionString + '&DBName=' + DBName + '&DBOName=' + DBOName + '&sesRoundOffId=' + sesRoundOffId + '&seslocname=' + seslocname;
        xmlhttp.onreadystatechange = stateChanged;
        xmlhttp.open("POST", url, true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        // xmlhttp.setRequestHeader("Content-length", str.length);
        // xmlhttp.setRequestHeader("Connection", "close")
        //alert(url + "url");
        //alert(str + "str");
        url = url + str;
        url = url + '&sesuserid=' + document.getElementById("sesuserid").value + '&sesusername=' + document.getElementById("sesusername").value;
        url = url + "&sid=" + Math.random();
        xmlhttp.send(str);
        //alert(url);
        //   alert(str+','+rid)
        //alert(url+','+str +','+rid);
    }
}


function senddata(pagen, str, rid) {
    set_gVal();
    if (seslocid == '') {
        alert("Your session has been expired.You need to login first.")
        window.location.href= "Default.aspx"
        return;
    }
    else {
        document.getElementById("serror").innerHTML = '';
        receiveid = rid;
        xmlhttp = GetXmlHttpObject();
        if (xmlhttp == null) {
            alert("Your browser does not support AJAX!");
            return;
        }
        var url;
        url = "ajax/" + pagen + ".aspx?";
        url = url + str;
      
        url = url + "&seslocid=" + seslocid + '&sesAcname=' + sesAcname + '&sesuserid=' + sesuserid + '&sesgrpid=' + sesgrpid + '&sesusername=' + sesusername + '&seslocationtype=' + seslocationtype + '&sescountryid=' + sescountryid + '&sesfinancialyear=' + sesfinancialyear + '&sesAccountId=' + sesAccountId + '&Conn1_ConnectionString=' + Conn1_ConnectionString + '&DBName=' + DBName + '&DBOName=' + DBOName + '&sesRoundOffId=' + sesRoundOffId + '&seslocname=' + seslocname;
        //url=url+'&seslocid='+document.getElementById("seslocid").value+'&sesAcname='+document.getElementById("sesAcname").value+'&sesuserid='+document.getElementById("sesuserid").value+'&sesgrpid='+document.getElementById("sesgrpid").value+'&sesusername='+document.getElementById("sesusername").value+'&seslocationtype='+document.getElementById("seslocationtype").value+'&sescountryid='+document.getElementById("sescountryid").value+'&sesfinancialyear='+document.getElementById("sesfinancialyear").value+'&sesAccountId='+document.getElementById("sesAccountId").value+'&Conn1_ConnectionString='+document.getElementById("Conn1_ConnectionString").value+'&DBName='+document.getElementById("DBName").value+'&DBOName='+document.getElementById("DBOName").value+'&sesRoundOffId='+document.getElementById("sesRoundOffId").value+'&seslocname='+document.getElementById("seslocname").value;
        url = url + "&sid=" + Math.random();
       // alert(url);
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.responseText == "" && document.getElementById('seslocid').value == "") { window.location.href = "Default.aspx"; } else { stateChanged(); }
            } else { stateChanged(); }
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
        numberonly();
    }
}

function senddatafrm(pagen, str, rid) {
    document.getElementById("serror").innerHTML = '';
    receiveid = rid;
    xmlhttp = GetXmlHttpObject();
    if (xmlhttp == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    var url;
    url = pagen + ".aspx?";
    url = url + str;
    //alert(url+str)
    xmlhttp.onreadystatechange = stateChanged;
    xmlhttp.open("GET", url, true);
    xmlhttp.send(null);
    numberonly();
}


//	function SupressError()	
//	{return true;}
//	window.onerror=SupressError;

//Error Handler	
function Errorhandler(description, pagename, lineno) {
    alert(
	'Syntax Help Error occurred! \n'
	+ '\n Description: \t' + description
	+ '\n Page Name:      \t' + pagename
	+ '\n At Line :       \t' + lineno
	)
    return true
}
//window.onerror=Errorhandler;

function toroman(num) {
    num = parseInt(num, 10);
    if (num > 0 && num < 6000) {
        var mill = ['', 'M', 'MM', 'MMM', 'MMMM', 'MMMMM'],
           cent = ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'],
           tens = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'],
           ones = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'],
           m, c, t, r = function (n) {
               n = (num - (num % n)) / n;
               return n;
           };
        m = r(1000);
        num = num % 1000;
        c = r(100);
        num = num % 100;
        t = r(10);
        return mill[m] + cent[c] + tens[t] + ones[num % 10];
    } else {
        return num;
    }
}

function getElementsByClsName(className, val) {
    var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
    var tag = "*";
    var elm = document;
    var elements = (tag == "*" && elm.all) ? elm.all : elm.getElementsByTagName(tag);
    var returnElements = [];
    var current;
    var length = elements.length;
    for (var i = 0; i < length; i++) {
        current = elements[i];
        if (testClass.test(current.className)) {
            returnElements.push(current);
            current.style.display = val;
        }
    }
}
function dropenquiry(enquiryid, locationid, replace) {

    if (confirm("Do You Want To Drop This Enquiry ?")) {
        senddata('Rpt_Enquiry', 'T=drop&enquiryid=' + enquiryid + '&locationid=' + locationid, replace);

    }

}
function GetXmlHttpObject() {
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    if (window.ActiveXObject) {
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}
function frmclose() { if (confirm("Are You Sure Want To Unload This Form?") == true) { location.href = 'home.aspx'; } }
function CSfrmclose() { if (confirm("Are You Sure Want To Unload This Form?") == true) { location.href = 'CShome.aspx'; } }
function pagesearch(val) {
    if (val == 's') { document.getElementById("pagecontent").style.display = 'none'; document.getElementById("searchcontent").style.display = 'block'; } else { document.getElementById("pagecontent").style.display = 'block'; document.getElementById("searchcontent").style.display = 'none'; }
}

function addcountry(cmbcountry, cmbstate, cmbcity) {
    var country, state, city;
    country = prompt("Enter the Country Name.", "");
    if (country == null) { return false; }
    state = prompt("Enter the State Name.", "");
    if (state == null) { return false; }
    city = prompt("Enter the City Name.", "");
    if (city == null) { return false; }
    if (country != null && state != null && city != null) {
        if (country.replace(/ /gi, "") != "" && state.replace(/ /gi, "") != "" && city.replace(/ /gi, "") != "") {
            senddata('addoption', 'T=country&cmbcountry=' + cmbcountry + '&cmbstate=' + cmbstate + '&cmbcity=' + cmbcity + '&country=' + replacehash(country) + '&state=' + replacehash(state) + '&city=' + replacehash(city), 'div' + cmbcountry);
        }
    }
}
function addstate(cmbcountry, cmbstate, cmbcity) {

    var state, city;
    var countryid = document.getElementById(cmbcountry).value;
    state = prompt("Enter the State Name.", "");
    if (state == null) { return false; }
    city = prompt("Enter the City Name.", "");
    if (state != null && city != null) {
        if (state.replace(/ /gi, "") != "" && city.replace(/ /gi, "") != "") {
            if (countryid != '' && countryid != 'A') {
                senddata('addoption', 'T=state&cmbcountry=' + cmbcountry + '&cmbstate=' + cmbstate + '&cmbcity=' + cmbcity + '&countryid=' + countryid + '&state=' + replacehash(state) + '&city=' + replacehash(city), 'div' + cmbstate);
            } else { alert("Please choose State First.") }
        }
    }
}


function addbank(cmbbank) {
    
    var bank;
    //var bankid = document.getElementById(cmbbank).value;
    bank = prompt("Enter the Bank Name.", "");
    if (bank == null) { return false; }

    if (bank != null) {
        if (bank.replace(/ /gi, "") != "") {
            senddata('addoption', 'T=bank&cmbbank=' + cmbbank + '&bank=' + replacehash(bank), 'divbank');
        }
    }
}

function addcity(cmbcountry, cmbstate, cmbcity) {
    var city;
    var stateid = document.getElementById(cmbstate).value;
    city = prompt("Enter the City Name.", "");
    if (city != null) {
        if (city.replace(/ /gi, "") != "") {
            if (stateid != '' && stateid != 'A') {
                senddata('addoption', 'T=city&cmbcountry=' + cmbcountry + '&cmbstate=' + cmbstate + '&cmbcity=' + cmbcity + '&stateid=' + stateid + '&city=' + replacehash(city), 'div' + cmbcity);
            } else { alert("Please choose State First.") }
        }
    }
}

function searchstate(cmbcountry, cmbstate, cmbcity) {
    if (document.getElementById(cmbcountry).value != 'A') {
        if (document.getElementById(cmbstate).value != 'A') { senddata('addoption', 'T=searchstate&countryid=' + document.getElementById(cmbcountry).value + '&stateid=' + document.getElementById(cmbstate).value + '&cmbcountry=' + cmbcountry + '&cmbstate=' + cmbstate + '&cmbcity=' + cmbcity, 'indiv' + cmbstate); } else { alert("Please choose State First."); }
    }
    else { alert("Please choose Country First.") }
}

function searchcity(cmbcountry, cmbstate, cmbcity) {
    if (document.getElementsByName(cmbstate)[0].value != 'A') {
        senddata('addoption', 'T=searchcity&stateid=' + document.getElementsByName(cmbstate)[0].value + '&cmbcountry=' + cmbcountry + '&cmbstate=' + cmbstate + '&cmbcity=' + cmbcity, 'div' + cmbcity);
    } else { alert("Please choose State First.") }
}


function addoptions(type, cmbid) {
    var flg;
    flg = prompt("Enter the " + type + " Name.", "");
    if (flg != null) {
        if (flg.replace(/ /gi, "") != "") {
            senddata('addoption', 'T=' + type + '&val=' + replacehash(flg) + '&cmbid=' + cmbid, 'div' + cmbid);
        }
    }
}
function addSubjects(type, cmbid, id) {
    var flg;
    flg = prompt("Enter the " + type + " Name.", "");
    if (flg != null) {
        if (flg.replace(/ /gi, "") != "") {
            senddata('addoption', 'T=' + type + '&val=' + replacehash(flg) + '&cmbid=' + cmbid + '&subtype=' + id, 'div' + cmbid);
        }
    }
}
function addremarks(type, cmbid, id) {
    var flg;
    flg = prompt("Enter the " + type + "", "");
    if (flg != null) {
        if (flg.replace(/ /gi, "") != "") {
            senddata('addoption', 'T=' + type + '&val=' + replacehash(flg) + '&cmbid=' + cmbid + '&id=' + id, 'div' + cmbid);
        }
    }
}

function addFloorbuilding(type, cmbid, locationid) {
    var flg;
    flg = prompt("Enter the " + type + " Name.", "");
    if (flg != null) {
        if (flg.replace(/ /gi, "") != "") {
            senddata('addoption', 'T=' + type + '&val=' + replacehash(flg) + '&cmbid=' + cmbid + '&locationid=' + locationid, 'div' + cmbid);
        }
    }
}

function addRoombuilding(type, cmbid, locationid) {
    var flg;
    flg = prompt("Enter the " + type + " Name.", "");
    if (flg != null) {
        if (flg.replace(/ /gi, "") != "") {
            senddata('addoption', 'T=' + type + '1&val=' + replacehash(flg) + '&cmbid=' + cmbid + '&locationid=' + locationid, 'div' + cmbid);
        }
    }
}

function addFloor(type, cmbid, buildingid, locationid) {
    var flg;
    flg = prompt("Enter the " + type + " Name.", "");
    if (flg != null) {
        if (flg.replace(/ /gi, "") != "") {
            senddata('addoption', 'T=' + type + '&val=' + replacehash(flg) + '&cmbid=' + cmbid + '&buildingid=' + buildingid + '&locationid=' + locationid, 'div' + cmbid);
        }
    }
}

function addCastCat(type, replaceId) {
    var flg;
    flg = prompt("Enter the " + type + " Name.", "");
    if (flg != null) {
        if (flg.replace(/ /gi, "") != "") {
            senddata('addoption', 'T=CastCategory&val=' + replacehash(flg), replaceId);
        }
    }
}
function getElementsByClassName(className, val) {
    var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
    var tag = "*";
    var elm = document;
    var elements = (tag == "*" && elm.all) ? elm.all : elm.getElementsByTagName(tag);
    var returnElements = [];
    var current;
    var length = elements.length;
    for (var i = 0; i < length; i++) {
        current = elements[i];
        if (testClass.test(current.className)) {
            returnElements.push(current);
            current.style.visibility = val;
        }
    }
}
function sort() {
    $(".tblist").tablesorter();
    $(".tbElist").tablesorter();
}
function sort1(id) {
    $("#" + id).tablesorter();
}

CheckVar = function (value) {
    try {

        value = parseFloat(value);
        if (value == '' || isNaN(value) || value == 'undefined' || value == ' ') {
            return 0;
        } else {
            return value;
        }
    } catch (e) {
        return 0;
    }
}
disabledefault = function () {
    var eleInput = document.getElementsByTagName('input');
    var eleSelect = document.getElementsByTagName('select');
    var eleTextArea = document.getElementsByTagName('textarea');
    for (var i = 0; i < eleInput.length - 1; i++) {
        if (eleInput[i].getAttribute("isdisable")) {
            var val = eleInput[i].getAttribute("isdisable");
            if (val == "Y") {
                eleInput[i].disabled = true;
            }
        }
    }
    for (var i = 0; i < eleInput.length - 1; i++) {
        if (eleSelect[i].getAttribute("isdisable")) {
            var val = eleSelect[i].getAttribute("isdisable");
            if (val == "Y") {
                eleSelect[i].disabled = true;
            }
        }
    }
    for (var i = 0; i < eleInput.length - 1; i++) {
        if (eleTextArea[i].getAttribute("isdisable")) {
            var val = eleTextArea[i].getAttribute("isdisable");
            if (val == "Y") {
                eleTextArea[i].disabled = true;
            }
        }
    }
}

